﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sagacity.Entity.Build;
using System.Collections.Generic;

namespace Sagacity.NC.Web.BasicSettings.Student
{
    public partial class StuZaixiaoDetailInfo : PageBase
    {
        public int mainResourceId = 0;
        public int schoolId = 0;
        public int studentId = 0;
        private int mStyleId = 0;

        private Sagacity.Build.Application.Resource mResource = new Sagacity.Build.Application.Resource();
        private Sagacity.Build.Role mRole = new Sagacity.Build.Role();
        private Sagacity.Entity.Build.ResourceStyleInfo mResStyleInfo = null;
        private Sagacity.Entity.Build.ResourceInfo mResInfo = null;
        private Sagacity.Entity.Build.RoleInfo mRoleInfo = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            mainResourceId = int.Parse(Request.QueryString["ResourceId"].ToString());
            studentId = int.Parse(Request.QueryString["StudentId"].ToString());   
            if (!IsPostBack)
            {
                GetStudentTitles();
                ShowSchoolSimpleInfo();
            }
        }

        private void GetStudentTitles()
        {
            List<Sagacity.Entity.Build.ResourceInfo> resources = new List<Sagacity.Entity.Build.ResourceInfo>();

            resources.Add(mResource.GetResourceInfoById(mainResourceId));
            resources[0].ControlType = 1;
            List<Sagacity.Entity.Build.ResourceInfo> childs = mResource.GetChildResourceInfos(mainResourceId, 1);
            resources.AddRange(childs);

            List<Sagacity.Entity.Build.ResourceMappingInfo> rMappInfo = mResource.GetChildResources(mainResourceId);

            foreach (var r in resources)
            {
                if (rMappInfo.Any(o => o.ChildResourceId == r.Id))
                {
                    var rm = rMappInfo.FirstOrDefault(o => o.ChildResourceId == r.Id);
                    r.ControlType = rm.ControlType;
                }
            }

            Repeater1.DataSource = resources;
            Repeater1.DataBind();
        }

        protected void ShowSchoolSimpleInfo()
        {
            mStyleId = int.Parse(hidStyleId.Value);
            mResStyleInfo = mResource.GetObject<Sagacity.Entity.Build.ResourceStyleInfo>(mStyleId);
            mResInfo = mResource.GetResrouse(mResStyleInfo.ResourceId);
            if (studentId == 0)
            {
                Literal1.Text = mResStyleInfo.Content;
            }
            else
            {
                CreateHtml();
            }
        }

        private string CreateHtmlById()
        {
            mResInfo.Model.Parameters.FirstOrDefault(o => o.ParameterName.ToLowerInvariant() == "student_id").RealValue = studentId;
            mRoleInfo = mRole.GetObject<RoleInfo>("from RoleInfo t where t.Id=" + base.CurrentUser.LoginRole.Id);
            return mResource.CreateStyleHTML(mResStyleInfo, mResInfo, mRoleInfo, "../Form/GetImage.aspx");
        }

        private void CreateHtml()
        {
            Literal1.Text = "";
            Literal1.Text = Literal1.Text + CreateHtmlById();
            Literal1.Text = Literal1.Text + "\r\n<p class='prln'></p>\r\n&nbsp;&nbsp;\r\n";
        }
    }
}
